<?php
/**
 * Create by chang
 * Date 2019/12/19
 * Time 16:00
 * 订单核销
 */

namespace app\api\controller\v2;

use app\common\controller\Api;
use app\common\controller\Queue;
use app\common\model\AgentShop;
use app\common\model\Commission;
use app\common\model\Order;
use app\common\model\Promotion;
use app\common\service\AgentService;
use app\common\service\DestoryService;

class Destory extends Api
{
	
	/**
	 * 创建临时核销订单并返回核销码
	 */
	public function createOrder()
	{
		$id = $this->request->post('id', '', 'intval');
		$number = $this->request->post('number', '', 'intval');
		if (!$id || !$number) {
			$this->error('参数错误');
		}
		
		$userId = $this->auth->id;
		$serverObj = new DestoryService();
		$res = $serverObj->createOrder($id, $userId, $number);
		if ($res['status']) {
			$this->success('创建成功', ['code' => $res['data']['code']]);
		}
		$this->error($res['msg']);
		
	}
	
	
	/**
	 * 代理核销
	 */
	public function destoryOrder()
	{
		$code = $this->request->post('code');
		!$code && $this->error('参数错误');
		
		$userId = $this->auth->id;
		$level = $this->auth->level;
		
		$serverObj = new AgentService();
		$authRes = $serverObj->checkDestoryAuth($userId, $level, $this->area);
		if (!$authRes['status']) {
			$this->error($authRes['msg']);
		}
		
		$destoryServer = new DestoryService();
		$orderRes = $destoryServer->getOrderByCode($code);
		
		if (!$orderRes['status']) {
			$this->error($orderRes['msg']);
		}
		
		$order = $orderRes['data']['order'];
		$agnetId = $authRes['data']['id'];
		if ($order['order_type'] == Order::TYPE_GIFT && $order['recommend_user_id']) {
			$promoter = Promotion::get(['user_id' => $order['user_id']]);
			$parentAgent = AgentShop::get($promoter['agent_shop_id']);
			if ($parentAgent && $agnetId != $parentAgent['id'] && $agnetId != $parentAgent['center_id']) {
				$this->error('该订单只能由归属上级核销');
			}
		}
		
		$type = $this->request->post('type', 'check');
		//查询订单信息
		if ($type == 'check') {
			$this->success('查询成功', $order);
		} else {
			//核销
			$force = $this->request->post('force', 0, 'intval');
			$res = $destoryServer->destructOrder($order, $orderRes['data']['destoryOrder'], $authRes['data'], $force);
			
			if ($res['status'] === true) {
				$goods = $orderRes['data']['order']['goods'][0];
				//添加库存检测任务
				Queue::addTask('checkStock', [
					'id' => $authRes['data']['id'],
					'goods_id' => $goods['goods_id'],
					'spec_id' => $goods['goods_info']['spec_id']
				]);
				
				$this->success("核销成功");
			} elseif ($res['status'] == -1) {
				$this->error($res['msg'], [], -1);
			} else {
				$this->error($res['msg']);
			}
		}
	}
	
	/**
	 * 获取用户待核销的商品及当前核销代理的库存
	 */
	
	public function getwaitdestorygoods()
	{
		//店铺用户ID
		$agentUserId = $this->request->get('agent_user_id');
		$service = new DestoryService();
		$res = $service->getUserDestoryGoodsAndAgentStore($this->auth->id, $agentUserId);
		if (!$res) {
			$this->error($service->getError());
		} else {
			$this->success('查询成功', $res);
		}
	}
	
	/**
	 * 用户自己核销商品
	 */
	public function userdestory()
	{
		$data = $this->request->post();
		if (empty($data['agent_user_id']) || empty($data['goods'])) {
			$this->error('参数错误');
		}
		
		$service = new DestoryService();
		$res = $service->userDestoryGoods($this->auth->id, $data['agent_user_id'], $data['goods']);
		if (!$res) {
			$this->error($service->getError());
		} else {
			$this->success('核销成功', $res);
		}
	}
}
